import plugin from '../../../lib/plugins/plugin.js'
import { Config, Render, Version } from '../components/index.js'
import { initImpart } from "../model/InitImpart.js"
import lodash from 'lodash'

let keys = lodash.map(Config.getCfgSchemaMap(), (i) => i.key)
let sysCfgReg = new RegExp(`^#?银趴设置\\s*(${keys.join('|')})?\\s*(.*)$`)

export class setting extends plugin {
    constructor() {
        super({
            name: '[impart-pro-plugin] 设置',
            dsc: '[impart-pro-plugin] 设置',
            event: 'message',
            priority: Config.priority,
            rule: [
                {
                    reg: '#?(开始|结束)银趴$',
                    fnc: 'modifyImpart',
                    permission: 'master'
                },
                {
                    reg: sysCfgReg,
                    fnc: 'setting',
                    permission: 'master'
                }
            ]
        })
    }

    async modifyImpart(e) {
        let reg = new RegExp('^#?(开始|结束)银趴$')
        let regRet = reg.exec(this.e.msg)
        if (!regRet) {
            return true
        }
        switch (regRet[1]) {
            case '开始':
                this.addImpartList(e)
                break
            case '结束':
                this.delImpartList(e)
                break
            default:
                break
        }
    }

    async setting(e) {
        let cfgReg = sysCfgReg
        let regRet = cfgReg.exec(e.msg)
        let cfgSchemaMap = Config.getCfgSchemaMap()
        if (!regRet) {
            return true
        }

        if (regRet[1]) {
            // 设置模式
            let val = regRet[2] || ''
            console.log(val)
            let cfgSchema = cfgSchemaMap[regRet[1]]
            if (cfgSchema.input) {
                val = cfgSchema.input(val)
            } else {
                val = cfgSchema.type === 'num' ? (val * 1 || cfgSchema.def) : !/关闭/.test(val)
            }
            if (val < 30) {
                this.reply('不能小于30'); // 回复用户
                return false
            } else {
                Config.modify(cfgSchema.fileName, cfgSchema.cfgKey, val)
            }
        }

        let schema = Config.getCfgSchema()
        let cfg = Config.getCfg()
        let imgPlus = false

        // 渲染图像
        return await Render.render('admin/index', {
            schema,
            cfg,
            imgPlus,
            isMiao: Version.isMiao
        }, { e, scale: 1.4 })
    }

    async addImpartList(e) {
        let value = e.group_id
        if ( initImpart(e) == true ) {
            this.reply(`此群已开始银趴`)
            return true
        } else {
            try {
                Config.modifyarr('impart-config', 'impartList', value)
                this.reply('操作成功~')
            } catch (error) {
                logger.error(error)
                this.reply('操作失败~')
            }
            return true
        }
    }

    async delImpartList(e) {
        let value = e.group_id
        if ( initImpart(e) == false ) {
            this.reply(`此群已结束银趴`)
            return true
        } else {
            try {
                Config.delServersArr(value)
                this.reply('操作成功~')
                return true
            } catch (error) {
                logger.error(error)
                this.reply('操作失败~')
                return true
            }
        }
    }

}